<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */

namespace app\api\controller;

use app\common\model\Article as ArticleModel;
use app\common\model\Material as MaterialModel;
use app\common\model\Card as CardModel;
use app\service\MsgService;

class Article extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /* 文章列表 */
    public function list()
    {
        $input = input();
        $page = max($input['page'], 1);
        $size = 20;
        $where = [];
        $list = ArticleModel::getPage($where, $page, $size, 'ord desc,id desc');
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        foreach ($list as $k => $v) {
            $list[$k]['picture'] = tomedia($v['picture']);
        }
        $this->ajax(200, 'success', $list);
    }

    /* 文章详情 */
    public function detail()
    {
        $input = input();
        if (!$input['id']) {
            $this->ajax(202, '参数错误');
        }
        $detail = ArticleModel::getOne(['id' => $input['id']]);
        if (!$detail) {
            $this->ajax(202, '参数错误');
        }
        $detail['picture'] = tomedia($detail['picture']);
        $this->ajax(200, 'success', $detail);
    }

    /* 素材列表 */
    public function materialList()
    {
        $input = input();
        $page = max($input['page'], 1);
        $size = 20;
        $where = [];
        $list = MaterialModel::getPage($where, $page, $size, 'ord desc,id desc');
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        foreach ($list as $k => $v) {
            $pics = json_decode($v['picture_list'], true);
            foreach ($pics as $kk => $vv) {
                $pics[$kk]['src'] = tomedia($vv['path']);
            }
            $list[$k]['picture_list'] = $pics;
        }
        $this->ajax(200, 'success', $list);
    }

    /* 单条素材 */
    public function materialDetail()
    {
        $input = input();
        if (!$input['id']) {
            $this->ajax(202, '参数错误');
        }
        $detail = MaterialModel::getOne(['id' => $input['id']]);
        if (!$detail) {
            $this->ajax(202, '参数错误');
        }
        $pics = json_decode($detail['picture_list'], true);
        foreach ($pics as $kk => $vv) {
            $pics[$kk]['src'] = tomedia($vv['path']);
        }
        $detail['picture_list'] = $pics;
        $this->ajax(200, 'success', $detail);
    }

    /* 邀请卡列表 */
    public function cardList()
    {
        $input = input();
        $page = max($input['page'], 1);
        $size = 100;
        $where = [];
        $list = CardModel::getPage($where, $page, $size, 'ord desc,id desc');
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        foreach ($list as $k => $v) {
            $list[$k]['bg_picture'] = tomedia($v['bg_picture']);
            $content = json_decode($v['content'], true);
            foreach ($content['list'] as $kk => $vv) {
                if ($vv['type'] == 'img') {
                    $content['list'][$kk]['content'] = tomedia($vv['content']);
                }
            }
            $list[$k]['content'] = $content;
        }
        $this->ajax(200, 'success', $list);
    }


    /**
     * 留言
     */
    public function message()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['content']) {
                return returnJson(202, '请填写留言内容');
            }

            if (!$data['id']) {
                return returnJson(202, '文章不存在');
            } else {
                $article = \app\common\model\Article::getOne(['id' => $data['id']]);
                if (!$article) {
                    return returnJson(202, '文章不存在');
                }
            }

            if (!$data['refer_id']) {
                return returnJson(202, '分享者不存在');
            } else {
                $refer_info = \app\common\model\User::getOne(['id' => $data['refer_id']]);
                if ($refer_info) {
                    if ($this->uid != $refer_info['id']) {
                        $msg_data = [
                            'first' => '您有新的留言',
                            'remark' => '请及时查看新留言',
                            'nickname' => $this->member['nickname'],
                            'name' => $article['title'],
                            'content' => $data['content'],
                            'time' => date('Y-m-d H:i:s')
                        ];
                        MsgService::S(['mall_id' => $refer_info['mall_id']])->sendTemp('NEW_MESSAGE', $refer_info['mall_id'], $refer_info['id'], $msg_data);
                        return returnJson(200, '留言成功');
                    } else {
                        return returnJson(202, '请不要给自己留言');
                    }
                } else {
                    return returnJson(202, '分享者不存在');
                }
            }
        }
    }


}